<template>
  <el-dialog
    top="10vh"
    width="500px"
    title="添加/编辑"
    :visible.sync="dialogValue"
    :close-on-click-modal="false"
    append-to-body
  >
    <SubmitForm
      :rule-form="ruleForm"
      :rules="rules"
      :form-list="formList"
      @change="handleFormChange"
      @save="onSave"
      @close="dialogValue = false"
    />

  </el-dialog>
</template>

<script>
import { http } from '@/api'
export default {
  name: 'FormAdd',
  props: {
    majorId: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      apiKey: 'course',
      dialogValue: false,
      ruleForm: {
      },
      defaultForm: {
        code: '',
        major_id: '',
        name: '',
        sort: 1,
        status: 1,
        type: '必修课程'
      },
      rules: {
        major_id: [{ required: true, message: '不能为空' }],
        name: [{ required: true, message: '不能为空' }],
        type: [{ required: true, message: '不能为空' }],
      },
      formList: [
        {
          name: 'select',
          prop: 'major_id',
          label: '专业',
          options: [

          ],
          disabled: () => {
            return !!(this.majorId);
          },
          colSpan: 24
        },
        {
          prop: 'code',
          name: 'input',
          label: '课程编码',
          colSpan: 24
        },
        {
          prop: 'type',
          name: 'select',
          label: '课程类型',
          colSpan: 24,
          options: [
            {
              label: '必修课程',
              value: '必修课程'
            },
            {
              label: '选修课程',
              value: '选修课程'
            }
          ]
        },
        {
          prop: 'name',
          name: 'input',
          label: '课程名称',
          colSpan: 24
        },
        {
          prop: 'sort',
          name: 'input',
          label: '排序',
          colSpan: 24
        },
        {
          prop: 'status',
          name: 'switch',
          label: '状态',
          colSpan: 24,
          activeValue: 1,
          inactiveValue: 0
        },
      ]
    }
  },
  methods: {
    handleFormChange({ key, val }) {

    },
    open(row) {
      console.log('row',row)
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data
          }
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm,
          major_id: this.majorId
        }
      }
      http.major.list().then(res => {
        this.formList.forEach(item => {
          if (item.prop === 'major_id') {
            item.options = res.data.map(item => {
              return {
                label: item.major,
                value: item.id
              }
            })
          }
        })
      })
    },
    onSave() {
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, {
        ...this.ruleForm,
      }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
